<template>
  <div class="bank-management">
    <!-- 页面头部 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
      <div>
        <h2 class="h4 mb-1">
          <i class="bi bi-bank2 text-primary me-2"></i>
          银行信息管理
        </h2>
        <p class="text-muted mb-0">管理自定义银行信息，扩展银行卡验证支持范围</p>
      </div>
      <div>
        <button class="btn btn-outline-primary me-2" @click="showHelp = !showHelp">
          <i class="bi bi-question-circle me-1"></i>
          使用说明
        </button>
        <button class="btn btn-primary" @click="testValidator">
          <i class="bi bi-check-circle me-1"></i>
          测试验证器
        </button>
      </div>
    </div>

    <!-- 使用说明 -->
    <div v-if="showHelp" class="alert alert-info mb-4">
      <div class="d-flex">
        <div class="flex-shrink-0">
          <i class="bi bi-info-circle-fill"></i>
        </div>
        <div class="flex-grow-1 ms-3">
          <h6 class="alert-heading">使用说明</h6>
          <ul class="mb-2">
            <li><strong>银行标准名称：</strong>银行的官方全称，如"中国邮政储蓄银行"</li>
            <li><strong>银行简称：</strong>常用的简化名称，如"邮储银行"</li>
            <li><strong>英文简称：</strong>银行的英文缩写，如"PSBC"</li>
            <li><strong>BIN码：</strong>银行卡号前6位，用于识别发卡银行</li>
            <li><strong>卡号长度规范：</strong>该银行卡号的标准长度，支持"16位"、"16-19位"、"16,19位"等格式</li>
            <li><strong>其他别名：</strong>用户可能输入的其他银行名称变体</li>
          </ul>
          <p class="mb-0">
            <strong>注意：</strong>添加的银行信息会自动集成到银行卡验证系统中，支持退款管理等功能的银行卡验证。
          </p>
        </div>
      </div>
    </div>

    <!-- 统计信息 -->
    <div class="row mb-4">
      <div class="col-md-3 col-sm-6 mb-3">
        <div class="card border-0 bg-primary bg-opacity-10">
          <div class="card-body text-center">
            <i class="bi bi-bank display-6 text-primary mb-2"></i>
            <h5 class="card-title text-primary mb-1">{{ bankStats.total }}</h5>
            <p class="card-text text-muted small mb-0">总银行数量</p>
          </div>
        </div>
      </div>
      <div class="col-md-3 col-sm-6 mb-3">
        <div class="card border-0 bg-success bg-opacity-10">
          <div class="card-body text-center">
            <i class="bi bi-plus-circle display-6 text-success mb-2"></i>
            <h5 class="card-title text-success mb-1">{{ bankStats.custom }}</h5>
            <p class="card-text text-muted small mb-0">自定义银行</p>
          </div>
        </div>
      </div>
      <div class="col-md-3 col-sm-6 mb-3">
        <div class="card border-0 bg-info bg-opacity-10">
          <div class="card-body text-center">
            <i class="bi bi-shield-check display-6 text-info mb-2"></i>
            <h5 class="card-title text-info mb-1">{{ bankStats.builtin }}</h5>
            <p class="card-text text-muted small mb-0">内置银行</p>
          </div>
        </div>
      </div>
      <div class="col-md-3 col-sm-6 mb-3">
        <div class="card border-0 bg-warning bg-opacity-10">
          <div class="card-body text-center">
            <i class="bi bi-credit-card display-6 text-warning mb-2"></i>
            <h5 class="card-title text-warning mb-1">{{ bankStats.withBin }}</h5>
            <p class="card-text text-muted small mb-0">支持BIN码</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 银行信息管理组件 -->
    <BankInfoManager @bank-updated="updateStats" />

    <!-- 验证器测试模态框 -->
    <div class="modal fade" id="testModal" tabindex="-1">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">
              <i class="bi bi-check-circle me-2"></i>
              银行卡验证器测试
            </h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
          </div>
          <div class="modal-body">
            <div class="row">
              <div class="col-md-6 mb-3">
                <label class="form-label">银行卡号</label>
                <input 
                  type="text" 
                  class="form-control" 
                  v-model="testData.cardNumber"
                  placeholder="请输入银行卡号"
                  @input="formatTestCardNumber"
                >
              </div>
              <div class="col-md-6 mb-3">
                <label class="form-label">银行名称</label>
                <input 
                  type="text" 
                  class="form-control" 
                  v-model="testData.bankName"
                  placeholder="请输入银行名称"
                  list="bankSuggestions"
                >
                <datalist id="bankSuggestions">
                  <option v-for="bank in allBanks" :key="bank.name" :value="bank.name">
                </datalist>
              </div>
            </div>
            
            <div class="d-flex justify-content-between align-items-center mb-3">
              <button class="btn btn-primary" @click="runTest">
                <i class="bi bi-play-fill me-1"></i>
                运行测试
              </button>
              <button class="btn btn-outline-secondary" @click="clearTest">
                <i class="bi bi-arrow-clockwise me-1"></i>
                清空
              </button>
            </div>
            
            <!-- 测试结果 -->
            <div v-if="testResult" class="mt-4">
              <h6 class="text-secondary mb-3">测试结果</h6>
              <div class="card">
                <div class="card-body">
                  <div class="row">
                    <div class="col-md-6">
                      <div class="d-flex align-items-center mb-2">
                        <span class="badge me-2" :class="testResult.valid ? 'bg-success' : 'bg-danger'">
                          <i :class="testResult.valid ? 'bi bi-check-lg' : 'bi bi-x-lg'"></i>
                        </span>
                        <strong>整体验证：</strong>
                        <span :class="testResult.valid ? 'text-success' : 'text-danger'" class="ms-2">
                          {{ testResult.valid ? '通过' : '失败' }}
                        </span>
                      </div>
                      
                      <div class="d-flex align-items-center mb-2">
                        <span class="badge me-2" :class="testResult.luhnValid ? 'bg-success' : 'bg-danger'">
                          <i :class="testResult.luhnValid ? 'bi bi-check-lg' : 'bi bi-x-lg'"></i>
                        </span>
                        <strong>Luhn验证：</strong>
                        <span :class="testResult.luhnValid ? 'text-success' : 'text-danger'" class="ms-2">
                          {{ testResult.luhnValid ? '通过' : '失败' }}
                        </span>
                      </div>
                      
                      <div class="d-flex align-items-center mb-2">
                        <span class="badge me-2" :class="testResult.bankMatched ? 'bg-success' : 'bg-danger'">
                          <i :class="testResult.bankMatched ? 'bi bi-check-lg' : 'bi bi-x-lg'"></i>
                        </span>
                        <strong>银行匹配：</strong>
                        <span :class="testResult.bankMatched ? 'text-success' : 'text-danger'" class="ms-2">
                          {{ testResult.bankMatched ? '匹配' : '不匹配' }}
                        </span>
                      </div>
                    </div>
                    
                    <div class="col-md-6">
                      <div v-if="testResult.standardBankName" class="mb-2">
                        <strong>标准银行名称：</strong>
                        <span class="text-primary">{{ testResult.standardBankName }}</span>
                      </div>
                      
                      <div v-if="testResult.bankInfo" class="mb-2">
                        <strong>银行信息：</strong>
                        <span class="badge" :class="testResult.bankInfo.isCustom ? 'bg-info' : 'bg-secondary'">
                          {{ testResult.bankInfo.isCustom ? '自定义银行' : '内置银行' }}
                        </span>
                      </div>
                      
                      <div v-if="testResult.bankInfo && testResult.bankInfo.customInfo" class="mb-2">
                        <small class="text-muted">
                          <div v-if="testResult.bankInfo.customInfo.binCode">
                            BIN码: {{ testResult.bankInfo.customInfo.binCode }}
                          </div>
                          <div v-if="testResult.bankInfo.customInfo.cardLengthRule">
                            卡号长度: {{ testResult.bankInfo.customInfo.cardLengthRule }}
                          </div>
                        </small>
                      </div>
                    </div>
                  </div>
                  
                  <div v-if="testResult.message" class="mt-3">
                    <div class="alert" :class="testResult.valid ? 'alert-success' : 'alert-warning'" role="alert">
                      <i :class="testResult.valid ? 'bi bi-check-circle-fill' : 'bi bi-exclamation-triangle-fill'" class="me-2"></i>
                      {{ testResult.message }}
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
              关闭
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { Modal } from 'bootstrap'
import BankInfoManager from '@/components/BankInfoManager.vue'
import { 
  validateBankCardEnhanced, 
  getAllSupportedBanks,
  formatCardNumber
} from '@/utils/enhancedBankCardValidator.js'

export default {
  name: 'BankManagement',
  components: {
    BankInfoManager
  },
  setup() {
    // 响应式数据
    const showHelp = ref(false)
    const allBanks = ref([])
    const testData = ref({
      cardNumber: '',
      bankName: ''
    })
    const testResult = ref(null)
    
    // 计算属性
    const bankStats = computed(() => {
      const banks = getAllSupportedBanks()
      const customWithBin = banks.custom.filter(b => b.binCode).length
      const builtinWithBin = banks.builtin.length // 假设内置银行都有BIN码
      
      return {
        total: banks.all.length,
        custom: banks.custom.length,
        builtin: banks.builtin.length,
        withBin: customWithBin + builtinWithBin
      }
    })
    
    // 方法
    const updateStats = () => {
      loadAllBanks()
    }
    
    const loadAllBanks = () => {
      const banks = getAllSupportedBanks()
      allBanks.value = banks.all
    }
    
    const testValidator = () => {
      const modal = new Modal(document.getElementById('testModal'))
      modal.show()
    }
    
    const formatTestCardNumber = () => {
      testData.value.cardNumber = formatCardNumber(testData.value.cardNumber)
    }
    
    const runTest = () => {
      if (!testData.value.cardNumber || !testData.value.bankName) {
        alert('请输入银行卡号和银行名称')
        return
      }
      
      testResult.value = validateBankCardEnhanced(
        testData.value.cardNumber,
        testData.value.bankName
      )
    }
    
    const clearTest = () => {
      testData.value = {
        cardNumber: '',
        bankName: ''
      }
      testResult.value = null
    }
    
    // 生命周期
    onMounted(() => {
      loadAllBanks()
      
      // 监听银行信息更新事件
      window.addEventListener('bankInfoUpdated', () => {
        loadAllBanks()
      })
    })
    
    return {
      showHelp,
      allBanks,
      bankStats,
      testData,
      testResult,
      updateStats,
      testValidator,
      formatTestCardNumber,
      runTest,
      clearTest
    }
  }
}
</script>

<style scoped>
.bank-management {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
}

.card {
  box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
  border: 1px solid rgba(0, 0, 0, 0.125);
}

.card-body {
  padding: 1.5rem;
}

.display-6 {
  font-size: 2rem;
}

.badge {
  font-size: 0.875em;
}

.alert {
  border: none;
  border-radius: 0.5rem;
}

.modal-lg {
  max-width: 900px;
}

@media (max-width: 768px) {
  .bank-management {
    padding: 10px;
  }
  
  .display-6 {
    font-size: 1.5rem;
  }
  
  .card-body {
    padding: 1rem;
  }
}
</style>